#include "CEffect.h"
#include "Wrappers\tinyxml.h"
#include "CEmitter.h"
#include "Wrappers\CSGD_TextureManager.h"
#include <d3dx9.h>
#include "Game Objects\CBase.h"
using namespace std;
CEffect::CEffect(void)
{
}

CEffect::~CEffect(void)
{
	for(unsigned int i = 0; i < m_lParticleEffects.size(); i++)
		delete m_lParticleEffects[i];
		//free(m_lParticleEffects[i]);
}

void CEffect::Play(int nPosX, int nPosY, int index)
{
	if(index < 0 || index >= (int)m_lParticleEffects.size())
		return;
	m_lParticleEffects[index]->setplaying(true);

	m_lParticleEffects[index]->setposX(nPosX);
	m_lParticleEffects[index]->setposY(nPosY);
}
void CEffect::Play(int index, CBase* object, int offx, int offy)
{
	if(index < 0 || index >= (int)m_lParticleEffects.size())
		return;
	m_lParticleEffects[index]->setplaying(true);

	m_lParticleEffects[index]->SetObject(object, offx, offy);

}

void CEffect::LoadEffect(std::string szFilename)
{
	TiXmlDocument doc;

	doc.LoadFile(szFilename.c_str());

	TiXmlElement *root = doc.RootElement();

	float fParticleLife = (float)atof((char*)root->FirstChildElement("life")->FirstChild()->Value());
	int nNumParticles = atoi(root->FirstChildElement("count")->FirstChild()->Value());
	int nEmitterWidth = atoi(root->FirstChildElement("width")->FirstChild()->Value());
	int nEmitterHeight = atoi(root->FirstChildElement("height")->FirstChild()->Value());
	int nPosX = atoi(root->FirstChildElement("posx")->FirstChild()->Value());
	int nPosY = atoi(root->FirstChildElement("posy")->FirstChild()->Value());
	float nVelX = (float)atof(root->FirstChildElement("velx")->FirstChild()->Value());
	float nVelY = (float)atof(root->FirstChildElement("vely")->FirstChild()->Value());
	int nAngle = atoi(root->FirstChildElement("angle")->FirstChild()->Value());
	int nBeginA = atoi(root->FirstChildElement("begina")->FirstChild()->Value());
	int nBeginR = atoi(root->FirstChildElement("beginr")->FirstChild()->Value());
	int nBeginG = atoi(root->FirstChildElement("beging")->FirstChild()->Value());
	int nBeginB = atoi(root->FirstChildElement("beginb")->FirstChild()->Value());
	float nDA = (float)atof(root->FirstChildElement("da")->FirstChild()->Value());
	float nDR = (float)atof(root->FirstChildElement("dr")->FirstChild()->Value());
	float nDG = (float)atof(root->FirstChildElement("dg")->FirstChild()->Value());
	float nDB = (float)atof(root->FirstChildElement("db")->FirstChild()->Value());
	float fStartParticleScale = (float)atof(root->FirstChildElement("beginscale")->FirstChild()->Value());
	float nDS = (float)atof(root->FirstChildElement("ds")->FirstChild()->Value());
	bool Looping;
	
	if(strcmp(root->FirstChildElement("looping")->FirstChild()->Value(), "true") == 0)
		Looping = true;
	else
		Looping = false;

	std::string filename = root->FirstChildElement("particle")->FirstChild()->Value();
	std::string rot = ".\\Resource\\Graphics\\";

	rot.append(filename.c_str());
	int source = atoi(root->FirstChildElement("source")->FirstChild()->Value());
	int dest = atoi(root->FirstChildElement("dest")->FirstChild()->Value());
	float aX = (float)atof(root->FirstChildElement("ax")->FirstChild()->Value());
	float aY = (float)atof(root->FirstChildElement("ay")->FirstChild()->Value());
	
	D3DXCOLOR color(0,0,0,255);
	int imageID = CSGD_TextureManager::GetInstance()->LoadTexture(rot.c_str(), color); 

	emit = new CEmitter(nNumParticles, nEmitterHeight, nEmitterWidth, fParticleLife, fStartParticleScale, nPosX, nPosY, imageID, nVelX, nVelY, nAngle, 
		(float)nBeginA, (float)nBeginR, (float)nBeginG , (float)nBeginB, nDA,  nDR, nDG, nDB, nDS, Looping, source, dest, aX, aY);

	m_lParticleEffects.push_back(emit);

}

void CEffect::update(float fElapsedTime)
{
	for(unsigned int i = 0; i < m_lParticleEffects.size(); i++)
	{
		if(m_lParticleEffects[i]->isitplaying())
		{
			m_lParticleEffects[i]->update(fElapsedTime);
		}
	}
}
void CEffect::render()
{
	for(unsigned int i = 0; i < m_lParticleEffects.size(); i++)
	{
		if(m_lParticleEffects[i]->isitplaying())
		{
			m_lParticleEffects[i]->render();
		}
	}
}
CEffect::CEffect(CEffect& copied)
{
	for(unsigned int i = 0; i < copied.m_lParticleEffects.size(); i++)
	{
		CEmitter* temp = new CEmitter(*copied.m_lParticleEffects[i]);
		m_lParticleEffects.push_back(temp);
	}
}